package com.itrip.auth.service.impl;

import com.itrip.auth.service.MailService;
import com.itrip.utils.dto.Dto;
import com.itrip.utils.vo.DtoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;

@Service
public class MailServiceImpl implements MailService {
    @Value("${spring.mail.username}")
    private String from;
    @Resource
    private JavaMailSender mailSender;

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public Dto sendSimpleMail(String to, String title, String content) {
        try {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(title);
        message.setText(content);
        mailSender.send(message);
        logger.info("邮件发送成功");
        return DtoUtil.returnSuccess();
        }catch (Exception e){
            return DtoUtil.returnFail("验证码发送失败");
        }
    }
}
